﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LevelEditor.Forms
{
    public partial class ProgressDialog : Form
    {
        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether the background operation can be canceled.
        /// Also shows/hides the cancel button.
        /// </summary>
        public bool Cancelable
        {
            set { cancelButton.Visible = ControlBox = value; }
            get { return cancelButton.Visible; }
        }

        /// <summary>
        /// Gets a value indicating whether the application has requested cancellation
        /// of the background operation.
        /// </summary>
        public bool Canceled
        {
            get { return backgroundWorker.CancellationPending; }
        }

        /// <summary>
        /// Minimum value of the progress bar.
        /// </summary>
        public int Minimum
        {
            set { ProgressBar.Minimum = value; }
            get { return ProgressBar.Minimum; }
        }

        /// <summary>
        /// Maximum value of the progress bar.
        /// </summary>
        public int Maximum
        {
            set { ProgressBar.Maximum = value; }
            get { return ProgressBar.Maximum; }
        }

        /// <summary>
        /// Get or set the value of the progress bar.
        /// </summary>
        public int Value
        {
            set { ProgressBar.Value = value; }
            get { return ProgressBar.Value; }
        }

        /// <summary>
        /// Gets or sets the manner in which progress should be indicated on the progress bar.
        /// </summary>
        /// <returns>One of the ProgressBarStyle values. The default is ProgressBarStyle.Blocks</returns>
        public ProgressBarStyle BarStyle
        {
            set { ProgressBar.Style = value; }
            get { return ProgressBar.Style; }
        }

        /// <summary>
        /// Show progress in taskbar.
        /// </summary>
        public bool ShowProgressInTaskbar
        {
            set { ProgressBar.ShowInTaskbar = value; }
            get { return ProgressBar.ShowInTaskbar; }
        }

        /// <summary>
        /// The text to display above the progress bar.
        /// </summary>
        public string Description
        {
            set { descriptionLabel.Text = value; }
            get { return descriptionLabel.Text; }
        }

        #endregion

        #region Constructors

        public ProgressDialog()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Start the worker thread.
        /// </summary>
        public void RunWorkerAsync()
        {
            backgroundWorker.RunWorkerAsync();
        }

        /// <summary>
        /// Start the worker thread.
        /// </summary>
        /// <param name="argument">A parameter for use by the background operation in the DoWork event handler.</param>
        public void RunWorkerAsync(Object argument)
        {
            backgroundWorker.RunWorkerAsync(argument);
        }

        /// <summary>
        /// Raises the ProgressChanged event.
        /// </summary>
        /// <param name="percentProgress">The value of the progress bar.</param>
        public void ReportProgress(int value)
        {
            System.Threading.Thread.Sleep(1);
            backgroundWorker.ReportProgress(value * 100 / Maximum);
        }

        /// <summary>
        /// Raises the ProgressChanged event.
        /// </summary>
        /// <param name="percentProgress">The value of the progress bar.</param>
        /// <param name="userState">The state object passed to RunWorkerAsync.</param>
        public void ReportProgress(int value, Object userState)
        {
            System.Threading.Thread.Sleep(1);
            backgroundWorker.ReportProgress(value * 100 / Maximum, userState);
        }

        #endregion

        #region Events

        public event DoWorkEventHandler DoWork;
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (DoWork != null)
                DoWork(sender, e);
        }

        public event ProgressChangedEventHandler ProgressChanged;
        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (ProgressChanged != null)
                ProgressChanged(sender, e);
        }

        public event RunWorkerCompletedEventHandler RunWorkerCompleted;
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (RunWorkerCompleted != null)
                RunWorkerCompleted(sender, e);
            DialogResult = DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Description = "Canceling operations.";
            ProgressBar.Style = ProgressBarStyle.Marquee;
            ProgressBar.State = wyDay.Controls.ProgressBarState.Error;
            backgroundWorker.CancelAsync();
            DialogResult = DialogResult.Cancel;
        }

        private void ProgressDialog_Shown(object sender, EventArgs e)
        {
            RunWorkerAsync(this);
        }

        #endregion

    }
}
